package ihistoryApp.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import ihistoryApp.dal.entity.User;
import ihistoryApp.dal.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 76004
 * @apiNote
 * @date 2024/5/25 20:31
 */
@Component
public class UserUtil {
    @Autowired
    private UserMapper userMapper;
    public void addExp(Long userId, long exp) {
        User user = userMapper.selectById(userId);
        exp = user.getExp() + exp;
        int userLevel = user.getLevel();
        long newLevel = userLevel + exp/getExpForLevel(userLevel);
        exp %= getExpForLevel(userLevel);
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper = new LambdaUpdateWrapper<User>()
                .eq(User::getUid, userId)
                .set(User::getExp, exp)
                .set(User::getLevel, newLevel)
                ;
        userMapper.update(null, userLambdaUpdateWrapper);
    }

    public void addScore(long uid, long score) {
        User user = userMapper.selectById(uid);
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper = new LambdaUpdateWrapper<User>()
                .eq(User::getUid, uid)
                .set(User::getScore, user.getScore() + score)
                ;
        userMapper.update(null, userLambdaUpdateWrapper);
    }

    private long getExpForLevel(int level) {
        return 50L * level;
    }
}
